import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
const app = getApp();
Page({
    data: {
        name: "",
        antistopOne: "",
        antistopTwo: "",
        show: false,
        columns: ['未分组', '一组', '二组', '三组', '四组', '观察组', '战术小队'],
        userGroup: '未分组',
        antistopExtra: '',
        dialogShow: false,
        act: "",
        admin: false
    },
    onReady() {
        const db = qq.cloud.database();
        const _ = db.command;
        db.collection("act")
            .where({
                'alliance': app.data.alliance
            })
            .get()
            .then((res) => {
                this.setData({ act: res.data[0].antistop.pop() });
            })
        db.collection('administer').where({
            unionId: _.eq(app.data.unionId)
        }).get().then((res) => {
            if (res.data.length != 0) {
                admin = true
                this.setData({ admin: true });
            }
        })
    },
    onShareAppMessage() {
        qq.showShareMenu({
            showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment']
        });
    },
    onChangeName(ev) {
        this.setData({ name: ev.detail });
    },
    onChangeAntistopOne(ev) {
        this.setData({ antistopOne: ev.detail });
    },
    onChangeAntistopTwo(ev) {
        this.setData({ antistopTwo: ev.detail });
    },
    onChangeAntistopExtra(ev) {
        this.setData({ antistopExtra: ev.detail });
    },
    affirm() {
        let This = this;
        const db = qq.cloud.database();
        const _ = db.command;
        this.setData({ dialogShow: true });
        Dialog.confirm({
                title: '请确认行动',
            })
            .then(() => {
                this.setData({ dialogShow: false });
                const db = qq.cloud.database();
                const _ = db.command;
                let userGroup = this.data.userGroup;
                let antistopOne = this.data.antistopOne;
                let antistopTwo = this.data.antistopTwo;
                let antistopExtra = this.data.antistopExtra;
                if ((antistopOne == "" && antistopTwo != "") || (antistopOne != "" && antistopTwo == "")) {
                    Toast('关键词1与关键词2不能其中一个为空');
                    return;
                }
                db.collection("act")
                    .where({
                        act: this.data.name,
                        'alliance': app.data.alliance
                    })
                    .get()
                    .then(async(res) => {
                        if (res.data.length > 0) {
                            Toast('不能设置重复的行动名');
                            return;
                        } else {
                            let admin = false;
                            await db.collection('administer').where({
                                unionId: _.eq(app.data.unionId)
                            }).get().then((res) => {
                                if (res.data.length != 0) {
                                    admin = true;
                                } else {
                                    Toast('您没有设置行动权限');
                                }
                            })
                            if (admin) {
                                if (this.data.name) {
                                    db.collection("act")
                                        .where({
                                            'alliance': app.data.alliance
                                        })
                                        .update({
                                            data: {
                                                act: _.push(this.data.name),
                                                timestamp: _.push(new Date().getTime()),
                                                antistop: _.push([{
                                                    [userGroup]: antistopExtra ? [antistopOne, antistopTwo, antistopExtra] : [antistopOne, antistopTwo]
                                                }])
                                            }
                                        })
                                        .then(res => {
                                            if (res.stats.updated == 0) {
                                                Toast('设置行动失败');
                                                This.setData({ name: "" });
                                                This.setData({ antistopOne: "" });
                                                This.setData({ antistopTwo: "" });
                                                This.setData({ antistopExtra: "" });
                                            } else {
                                                Toast('行动设置成功');
                                                This.setData({ name: "" });
                                                This.setData({ antistopOne: "" });
                                                This.setData({ antistopTwo: "" });
                                                This.setData({ antistopExtra: "" });
                                            }

                                        }).catch(err => {
                                            Toast('设置行动失败');
                                        })
                                } else {
                                    let arrayKeyword;
                                    await db.collection("act")
                                        .where({
                                            'alliance': app.data.alliance
                                        })
                                        .get().then(res => {
                                            arrayKeyword = res.data[0].antistop;
                                        }).catch(err => {
                                            console.log(err);
                                        })
                                    if (this.data.antistopOne != "" && this.data.antistopTwo != "") {
                                        arrayKeyword[arrayKeyword.length - 1][userGroup] = antistopExtra ? [antistopOne, antistopTwo, antistopExtra] : [antistopOne, antistopTwo];
                                    } else {
                                        arrayKeyword[arrayKeyword.length - 1][userGroup].push(antistopExtra);
                                    }
                                    db.collection("act")
                                        .where({
                                            'alliance': app.data.alliance
                                        })
                                        .update({
                                            data: {
                                                antistop: arrayKeyword
                                            }
                                        })
                                        .then(res => {
                                            db.collection("act")
                                                .where({
                                                    'alliance': app.data.alliance
                                                })
                                                .get()
                                                .then((res) => {
                                                    this.setData({ act: res.data[0].antistop.pop() });
                                                })
                                            if (res.stats.updated == 0) {
                                                Toast('设置行动失败');
                                                This.setData({ name: "" });
                                                This.setData({ antistopOne: "" });
                                                This.setData({ antistopTwo: "" });
                                                This.setData({ antistopExtra: "" });
                                            } else {
                                                Toast('行动设置成功');
                                                This.setData({ name: "" });
                                                This.setData({ antistopOne: "" });
                                                This.setData({ antistopTwo: "" });
                                                This.setData({ antistopExtra: "" });
                                            }

                                        }).catch(err => {
                                            Toast('设置行动失败');
                                        })
                                }
                            }
                        }
                    }).catch(err => {});

            })
            .catch(() => {
                this.setData({ dialogShow: false });
            });

    },
    onClose() {
        this.setData({ show: false });
    },
    onChange(event) {
        const { picker, value, index } = event.detail;
        this.setData({ userGroup: value });
    },
    onClickLeft() {
        this.setData({ show: false });
    },
    async onClickRight() {
        this.setData({ show: false });
    },
    setGroup() {
        this.setData({ show: true });
    }
})